package io.geekidea.boot;

import io.geekidea.boot.util.IpUtil;
import org.apache.commons.collections4.CollectionUtils;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.Banner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.EnableAspectJAutoProxy;
import org.springframework.core.env.ConfigurableEnvironment;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.transaction.annotation.EnableTransactionManagement;

import java.util.List;

/**
 * 启动类
 *
 * @author geekidea
 * @date 2022-3-16
 */
@EnableAsync
@EnableAspectJAutoProxy(exposeProxy = true)
@SpringBootApplication(scanBasePackages = {"io.geekidea.boot", "com.hwtx.form"})
@MapperScan(basePackages = {"com.hwtx.form.persistence", "io.geekidea.boot.*.mapper"})
@EnableCaching  // 必须添加此注解
@EnableTransactionManagement
public class SpringBootPlusApplication {

    private static final String BACKSLASH = "/";

    public static void main(String[] args) {
        SpringApplication app = new SpringApplication(SpringBootPlusApplication.class);
        app.setBannerMode(Banner.Mode.OFF);
        System.setProperty("pagehelper.banner", "false");
        printlnProjectInfo(app.run(args));
    }

    /**
     * 打印项目信息
     */
    private static void printlnProjectInfo(ConfigurableApplicationContext context) {
        try {
            ConfigurableEnvironment environment = context.getEnvironment();
            String serverPort = environment.getProperty("server.port");
            String contextPath = environment.getProperty("server.servlet.context-path");
            if (!BACKSLASH.equals(contextPath)) {
                contextPath = contextPath + BACKSLASH;
            }
            String localhostDocUrl = "\nhttp://localhost:" + serverPort + contextPath + "doc.html";
            System.out.println(localhostDocUrl);
            String localhostSwaggerUrl = "http://localhost:" + serverPort + contextPath + "swagger-ui/index.html";
            System.out.println(localhostSwaggerUrl);
            List<String> ipV4List = IpUtil.getLocalhostIpList();
            if (CollectionUtils.isNotEmpty(ipV4List)) {
                for (String ip : ipV4List) {
                    String ipUrl = "http://" + ip + ":" + serverPort + contextPath + "doc.html";
                    System.out.println(ipUrl);
                }
            }
            System.out.println("\n账号：admin");
            System.out.println("密码：123456");
            System.out.println("swagger密码：e10adc3949ba59abbe56e057f20f883e\n");
        } catch (Exception e) {
        }
    }

}
